Skip to content

mcpx: structured upstream-error carriage on the ask path + local-error origin marker (#2435) - #9038

Merged
dezren39 merged 3 commits into
mainfrom
queue/8625-2435-ask-upstream
Nov 12, 2026
Merged

dezren39 merged 3 commits into
mainfrom
queue/8625-2435-ask-upstream

Conversation

@dezren39

@dezren39 dezren39 commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

What

The #2435 Phase 1 remainder (the daemon /v1 structured carriage and the direct gateway relay already landed):

(a) Ask path relays the structured upstream error verbatim. daemonAsker.Poll built Outcome.Upstream from the text heuristic (upstreamError, string-matching the rendered error) even though the reply's structured upstream field was in hand — losing data entirely (the heuristic recovers only code + message). Poll now prefers reply.Upstream verbatim (code, message, data) and keeps the heuristic as the fallback for replies without the field. The -32021 classification already read the structured field; now the relayed error matches it.

(b) Local-error origin marker. Every error mcpx mints itself on the MCP surface now carries "mcpx": {"origin": "local"} merged into its data. Implementation: rpcError gains an unexported relayed flag set only by UpstreamError.relay; HandleOn stamps the marker via withLocalOrigin on every non-relayed error response — one minting point covering name checks (-32602), internal failures (-32603), policy refusals, and the rest of the dispatch surface — plus direct stamps on the pre-dispatch framing errors (HTTP/stdio parse, invalid-request, header and session checks) that bypass HandleOn. Peer errors are relayed byte-identical and never marked. Data that is not a JSON object has nowhere to merge a key and is left as-is (every current local mint carries nil or object data).

Cross-repo check (per the packet): dezren39/nix vendors no copy of these files (GitHub code search for the package in that repo: 0 hits; it consumes mcpx from this repo), so there is nothing to port.

Tests

  • internal/cli: TestAskPollPrefersTheStructuredUpstreamError (verbatim code/message/data over a differing text rendering), TestAskPollMissingCapabilityKeepsItsData (-32021 keeps requiredCapabilities in both the classified error and the relayed upstream), TestAskPollFallsBackToTheTextHeuristic (no-field replies unchanged, no invented data).
  • internal/mcpserver: TestALocallyMintedErrorCarriesTheLocalOriginMarker (marker merged into notFound's existing {uri} data), TestALocallyMintedErrorWithoutDataIsMarkedToo, TestARelayedPeerErrorIsNeverMarkedLocal (relayed -32602 with peer data arrives byte-identical, unmarked) — the ask-path/gateway parity the issue asks for is asserted from both sides: same error shape, origin marker the only difference, and it appears only on mcpx's own.
  • Full internal/mcpserver and internal/cli suites pass locally; gofmt clean. (The pre-existing main-baseline failures in internal/settings/internal/e2e — mcpx baseline: four tests red on main (settings docs/scriptenv + e2e envguard classification) #9017 — are untouched by this change and will show on CI as they do for every mcpx PR.)

Closes #2435.


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

@dezren39
dezren39 requested a review from a team as a code owner November 12, 2026 21:20
@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 29 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 20def5aa-775c-4f76-a98e-9f9203d28eac
Commits

Reviewing files that changed from the base of the PR and between 0941f75 and 4d05018.

Files selected for processing: 9

  • pkgs/mcpx/internal/cli/serve_ask.go
  • pkgs/mcpx/internal/cli/serve_ask_upstream_test.go
  • pkgs/mcpx/internal/mcpserver/capability.go
  • pkgs/mcpx/internal/mcpserver/origin_marker_codex_test.go
  • pkgs/mcpx/internal/mcpserver/origin_marker_test.go
  • pkgs/mcpx/internal/mcpserver/server.go
  • pkgs/mcpx/internal/mcpserver/tasks.go
  • pkgs/mcpx/internal/mcpserver/transport.go
  • pkgs/mcpx/internal/tasks/tasks.go
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T22:48:42.715873Z 4d05018 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bad7d6aea2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +788 to +792
if resp != nil && resp.Error != nil && !resp.Error.relayed {
// An error mcpx minted itself, anywhere in the dispatch
// below: stamp its local origin (#2435). A relayed peer
// error keeps the peer's data exactly as it arrived.
resp.Error = withLocalOrigin(resp.Error)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve relay provenance through task execution

When an upstream error passes through task execution, startTask copies only code, message and data into tasks.Fault (tasks.go:109–112). Both the eager completion path (tasks.go:367–369) and legacy tasks/result (tasks.go:698–700) reconstruct an rpcError without relayed, so this new check stamps a peer's error as local and modifies its data. Preserve the provenance through fault storage and reconstruction so task-backed upstream failures remain unchanged, just like direct failures.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 16959a3 (queue task 8625, lane code-d).

tasks.Fault now carries the provenance end to end: a new Relayed field (persisted in the fault JSON, so it survives the store and its database round-trip) is set at both fault mint sites — startTask's closure copies err.relayed, and driveAsked copies resp.Error.relayed — and both reconstruction sites (the eager completion path and legacy tasks/result) rebuild the rpcError with relayed: fault.Relayed, so HandleOn leaves a task-backed peer error byte-identical and unmarked, exactly like the direct path.

Failing-first tests (they reproduced this finding before the fix): TestARelayedPeerErrorThroughAnEagerTaskKeepsItsData, TestARelayedPeerErrorThroughTasksResultKeepsItsData, TestARelayedPeerErrorThroughAnAskedTaskKeepsItsData — each asserts the upstream's code/message/data arrive unchanged with no mcpx marker. Full internal/mcpserver, internal/tasks and internal/cli suites pass on the laptop.

Comment on lines 2247 to +2248
writeJSON(w, http.StatusBadRequest, response{JSONRPC: "2.0",
Error: &rpcError{Code: codeParse, Message: err.Error()}})
Error: withLocalOrigin(&rpcError{Code: codeParse, Message: err.Error()})})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Mark the remaining transport-local error responses

For an HTTP POST containing [], the adjacent empty-batch branch still returns an unmarked local error. Other pre-dispatch responses also bypass HandleOn, including rejected Origins (server.go:2064), DELETE session failures (server.go:2302–2308) and batchRefused (transport.go:125–128). Consequently, clients cannot consistently use the new marker to distinguish mcpx refusals from upstream errors. Stamp these remaining transport responses and add a table-driven guard covering the error families rather than only the two dispatch cases in the new tests.

AGENTS.md reference: pkgs/mcpx/internal/AGENTS.md:L11-L12

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 16959a3 (queue task 8625, lane code-d).

The remaining pre-dispatch responses are now stamped withLocalOrigin at their mint sites, so no caller can bypass the stamp: batchRefused itself (covers the HTTP modern/legacy refusals and the stdio batch path), the three runBatch element-framing errors (unparseable element, initialize-in-batch, per-request-metadata element), serveBatch's empty-batch and unknown-session errors, ServeHTTP's rejected-Origin response, both serveDELETE errors, and the serveGET family (the notAllowed closure, unknown session, and the second-stream conflict).

Class guard, per the repo's AGENTS.md rule for a class of bug: TestTransportLocalErrorsCarryTheLocalOriginMarker is table-driven over eleven transport-local error families and fails if any one of them ships unmarked. It reproduced this finding on every case before the fix and passes after it; full internal/mcpserver suite green on the laptop.

…ocal errors (#2435)

Codex review on PR #9038, two P2 findings: (1) task execution flattened a relayed peer error into a plain fault, so the eager and tasks/result reconstructions were stamped with the local-origin marker and the peer data mutated - tasks.Fault now carries Relayed (persisted in fault JSON) from both fault mint sites (startTask, driveAsked) to both reconstruction sites. (2) Pre-dispatch transport errors bypassed HandleOn and went unmarked - batchRefused, runBatch element framing, serveBatch empty/unknown-session, ServeHTTP rejected Origin, serveDELETE and serveGET refusals now stamp withLocalOrigin at the mint. Failing-first tests: relayed data byte-identical through eager, tasks/result and asked tasks; table guard over eleven transport-local error families.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 16959a3853

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +468 to +470
case map[string]any:
if inner, ok := d["mcpx"].(map[string]any); ok {
inner["origin"] = "local"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Copy error data before adding the origin marker

When two clients concurrently retrieve tasks/result for the same locally failed task, this mutates a shared map without synchronization. runInner already calls HandleOn, so the stored fault contains the marker map; Store.Result returns that same fault, and each tasks/result response reuses fault.Data before calling this function again. Consequently, concurrent retrievals write inner["origin"] simultaneously, potentially causing a fatal concurrent-map-write error or racing with response serialization. Copy both the outer data map and the nested mcpx map before modifying them.

Useful? React with 👍 / 👎.

@dezren39
dezren39 merged commit ca0b691 into main Nov 12, 2026
14 of 15 checks passed
@dezren39
dezren39 deleted the queue/8625-2435-ask-upstream branch November 12, 2026 22:48
@dezren39

Copy link
Copy Markdown
Member Author

agent-note:v1
state: salvage-check
task: 17273
lane: code-d
worker: repo-worker-code
What: Salvage diff for duplicate PR #7885 (mcpx structured upstream-error carriage + local-error origin marker, #2435), which is being closed as superseded by this PR (#9038, merged ca0b691). Behavior-by-behavior comparison of the two diffs: structured-upstream preference — #7885 TestAskPollPrefersStructuredUpstreamError vs #9038 TestAskPollPrefersTheStructuredUpstreamError (covered); text fallback for older daemons — #7885 TestAskPollFallsBackToTextForOlderDaemonReply vs #9038 TestAskPollFallsBackToTheTextHeuristic (covered); -32021 data carriage — #7885 parity test vs #9038 TestAskPollMissingCapabilityKeepsItsData plus the eager/asked task relay tests (covered, broader); local origin marker minted, merged into existing data, absent on relayed errors — #7885 local_error.go mintLocalError tests vs #9038 withLocalOrigin + origin_marker_test.go + origin_marker_codex_test.go incl. transport-local errors (covered, broader). The only structural difference is placement (dedicated local_error.go vs server/transport), not behavior.
Salvage: nothing unique in PR #7885 to port.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mcpx: Phase 1 of #2434 — structured upstream-error carriage through the gateway + local-error origin marker

1 participant